package com.xuxu.blog.controller;

import java.util.Arrays;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xuxu.blog.entity.SignEntity;
import com.xuxu.blog.service.SignService;
import com.xuxu.common.utils.PageUtils;
import com.xuxu.common.utils.R;

/**
 * 
 *
 * @author maxuxu
 * @email maxuxu019@gmail.com
 * @date 2022-08-21 22:22:31
 */
@RestController
@RequestMapping("blog/sign")
@Api(value = "SignController",tags = "打卡签到")
@Slf4j
public class SignController {
    @Autowired
    private SignService signService;

    @PostMapping("/insert")
    public R insert(@RequestBody SignEntity sign) {
        log.info("接收到的实体类：{}", sign);
        boolean save = signService.save(sign);
        if (save) {
            return R.ok("发送成功").put("code",20000);
        }
        return R.error("签到失败");
    }

    /**
     * 验证用户今天是否已经签到过了
     * @param username
     * @return
     */
    @RequestMapping("/selectIsSigned/{username}")
    public R selectIsSigned(@PathVariable("username") String username ) {
        boolean signed = signService.isSigned(username);
        if (signed) {
            return R.error("今天已经签到过了");
        }
        return R.ok("成功签到").put("data", "签到成功");
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
            public R list(@RequestParam Map<String, Object> params){
        PageUtils page = signService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
            public R info(@PathVariable("id") Integer id){
		SignEntity sign = signService.getById(id);

        return R.ok().put("sign", sign);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
            public R save(@RequestBody SignEntity sign){
		signService.save(sign);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
            public R update(@RequestBody SignEntity sign){
		signService.updateById(sign);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
            public R delete(@RequestBody Integer[] ids){
		signService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
